﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using Mss.Common.Helpers.SelectableCollection;
using Mss.Common.Interfaces;
using Mss.Managers;

namespace Mss.UiCommon.Controls.TopMenu
{
    public class TopMenuItem : ToggleButton
    {
        /// <summary>
        /// Using a DependencyProperty as the backing store for Color.  
        /// This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty ColorProperty =
            DependencyProperty.Register("Color", typeof (Brush), typeof (TopMenuItem),
                                        new UIPropertyMetadata(null));

        /// <summary>
        /// Using a DependencyProperty as the backing store for ViewUri.  
        /// This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty ViewUriProperty =
            DependencyProperty.Register("ViewUri", typeof(string), typeof(TopMenuItem), 
                                        new UIPropertyMetadata(string.Empty));

        private IView _view;

        #region Constructors

        static TopMenuItem()
        {
            //Apply default style.
            DefaultStyleKeyProperty.OverrideMetadata(typeof (TopMenuItem),
                                                     new FrameworkPropertyMetadata(typeof (TopMenuItem)));
        }

        #endregion

        public string ViewUri
        {
            get { return (string)GetValue(ViewUriProperty); }
            set { SetValue(ViewUriProperty, value); }
        }

        [Category("Brushes")]
        public Brush Color
        {
            get { return (Brush) GetValue(ColorProperty); }
            set { SetValue(ColorProperty, value); }
        }

        protected override void OnChecked(RoutedEventArgs e)
        {
            base.OnChecked(e);

            if (string.IsNullOrEmpty(ViewUri)) return;

            if (_view == null)
            {
                _view = (IView) Application.LoadComponent(new Uri(ViewUri, UriKind.Relative));
            }

            NavigationManager.Current.Navigate(_view);
        }
    }
}